Trò chơi Angry Birds trong UNITY Engine
31.686 lượt xem;
- GoTweenUtils.cs
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Reflection;
5
6
7 public static class GoTweenUtils
8 {
9 /// <summary>
10 /// fetches the actual function for the given ease type
11 /// </summary>
12 public static Func<float,float,float,float,float> easeFunctionForType( GoEaseType easeType )
13 {
14 switch( easeType )
15 {
16 case GoEaseType.Linear:
17 return GoEaseLinear.EaseNone;
18
19 case GoEaseType.BackIn:
20 return GoEaseBack.EaseIn;
21 case GoEaseType.BackOut:
22 return GoEaseBack.EaseOut;
23 case GoEaseType.BackInOut:
24 return GoEaseBack.EaseInOut;
25
26 case GoEaseType.BounceIn:
27 return GoEaseBounce.EaseIn;
28 case GoEaseType.BounceOut:
29 return GoEaseBounce.EaseOut;
30 case GoEaseType.BounceInOut:
31 return GoEaseBounce.EaseInOut;
32
33 case GoEaseType.CircIn:
34 return GoEaseCircular.EaseIn;
35 case GoEaseType.CircOut:
36 return GoEaseCircular.EaseOut;
37 case GoEaseType.CircInOut:
38 return GoEaseCircular.EaseInOut;
39
40 case GoEaseType.CubicIn:
41 return GoEaseCubic.EaseIn;
42 case GoEaseType.CubicOut:
43 return GoEaseCubic.EaseOut;
44 case GoEaseType.CubicInOut:
45 return GoEaseCubic.EaseInOut;
46
47 case GoEaseType.ElasticIn:
48 return GoEaseElastic.EaseIn;
49 case GoEaseType.ElasticOut:
50 return GoEaseElastic.EaseOut;
51 case GoEaseType.ElasticInOut:
52 return GoEaseElastic.EaseInOut;
53 case GoEaseType.Punch:
54 return GoEaseElastic.Punch;
55
56 case GoEaseType.ExpoIn:
57 return GoEaseExponential.EaseIn;
58 case GoEaseType.ExpoOut:
59 return GoEaseExponential.EaseOut;
60 case GoEaseType.ExpoInOut:
61 return GoEaseExponential.EaseInOut;
62
63 case GoEaseType.QuadIn:
64 return GoEaseQuadratic.EaseIn;
65 case GoEaseType.QuadOut:
66 return GoEaseQuadratic.EaseOut;
67 case GoEaseType.QuadInOut:
68 return GoEaseQuadratic.EaseInOut;
69
70 case GoEaseType.QuartIn:
71 return GoEaseQuartic.EaseIn;
72 case GoEaseType.QuartOut:
73 return GoEaseQuartic.EaseOut;
74 case GoEaseType.QuartInOut:
75 return GoEaseQuartic.EaseInOut;
76
77 case GoEaseType.QuintIn:
78 return GoEaseQuintic.EaseIn;
79 case GoEaseType.QuintOut:
80 return GoEaseQuintic.EaseOut;
81 case GoEaseType.QuintInOut:
82 return GoEaseQuintic.EaseInOut;
83
84 case GoEaseType.SineIn:
85 return GoEaseSinusoidal.EaseIn;
86 case GoEaseType.SineOut:
87 return GoEaseSinusoidal.EaseOut;
88 case GoEaseType.SineInOut:
89 return GoEaseSinusoidal.EaseInOut;
90 }
91
92 return GoEaseLinear.EaseNone;
93 }
94
95
96 /// <summary>
97 /// either returns a super fast Delegate to set the given property or null if it couldn't be found
98 /// via reflection
99 /// </summary>
100 public static T setterForProperty<T>( System.Object targetObject, string propertyName )
101 {
102 // first get the property
103 #if NETFX_CORE
104 var propInfo = targetObject.GetType().GetRuntimeProperty( propertyName );
105 #else
106 var propInfo = targetObject.GetType().GetProperty( propertyName );
107 #endif
108
109 if( propInfo == null )
110 {
111 Debug.Log( "could not find property with name: " + propertyName );
112 return default( T );
113 }
114
115 #if NETFX_CORE
116 // Windows Phone/Store new API
117 return (T)(object)propInfo.SetMethod.CreateDelegate( typeof( T ), targetObject );
118 #else
119 return (T)(object)Delegate.CreateDelegate( typeof( T ), targetObject, propInfo.GetSetMethod() );
120 #endif
121 }
122
123
124 /// <summary>
125 /// either returns a super fast Delegate to get the given property or null if it couldn't be found
126 /// via reflection
127 /// </summary>
128 public static T getterForProperty<T>( System.Object targetObject, string propertyName )
129 {
130 // first get the property
131 #if NETFX_CORE
132 var propInfo = targetObject.GetType().GetRuntimeProperty( propertyName );
133 #else
134 var propInfo = targetObject.GetType().GetProperty( propertyName );
135 #endif
136
137 if( propInfo == null )
138 {
139 Debug.Log( "could not find property with name: " + propertyName );
140 return default( T );
141 }
142
143 #if NETFX_CORE
144 // Windows Phone/Store new API
145 return (T)(object)propInfo.GetMethod.CreateDelegate( typeof( T ), targetObject );
146 #else
147 return (T)(object)Delegate.CreateDelegate( typeof( T ), targetObject, propInfo.GetGetMethod() );
148 #endif
149 }
150
151
152 #region math functions
153
154 /// <summary>
155 /// note for all lerps: normally a lerp would be something like the following:
156 /// val1 + ( val2 - val1 ) * t
157 /// or in more familiar terms:
158 /// start + ( end - start ) * t
159 ///
160 /// when lerping relatively, the formula simplifies to:
161 /// start + end * t
162 ///
163 /// for all the unclamped lerps in this class the diff value is precalculated and cached. that means these arent like normal
164 /// lerps where you pass in the start and end values. the "diff" paramter in each method should be either the cached
165 /// ( end - start ) for non-relative tweens or just end for relative tweens (that are not "from" tweens)
166 /// </summary>
167
168
169 /// <summary>
170 /// unclamped lerp from c1 to c2. diff should be c2 - c1 (or just c2 for relative lerps)
171 /// </summary>
172 public static Color unclampedColorLerp( Color c1, Color diff, float value )
173 {
174 return new Color
175 (
176 c1.r + diff.r * value,
177 c1.g + diff.g * value,
178 c1.b + diff.b * value,
179 c1.a + diff.a * value
180 );
181 }
182
183
184 /// <summary>
185 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
186 /// </summary>
187 public static Vector2 unclampedVector2Lerp( Vector2 v1, Vector2 diff, float value )
188 {
189 return new Vector2
190 (
191 v1.x + diff.x * value,
192 v1.y + diff.y * value
193 );
194 }
195
196
197 /// <summary>
198 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
199 /// </summary>
200 public static Vector3 unclampedVector3Lerp( Vector3 v1, Vector3 diff, float value )
201 {
202 return new Vector3
203 (
204 v1.x + diff.x * value,
205 v1.y + diff.y * value,
206 v1.z + diff.z * value
207 );
208
209 /*
210 return new Vector3
211 (
212 v1.x + ( v2.x - v1.x ) * value,
213 v1.y + ( v2.y - v1.y ) * value,
214 v1.z + ( v2.z - v1.z ) * value
215 );
216 */
217 }
218
219
220 /// <summary>
221 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
222 /// </summary>
223 public static Vector4 unclampedVector4Lerp( Vector4 v1, Vector4 diff, float value )
224 {
225 return new Vector4
226 (
227 v1.x + diff.x * value,
228 v1.y + diff.y * value,
229 v1.z + diff.z * value,
230 v1.w + diff.w * value
231 );
232 }
233
234 #endregion
235
236 }
2 using System;
3 using System.Collections;
4 using System.Reflection;
5
6
7 public static class GoTweenUtils
8 {
9 /// <summary>
10 /// fetches the actual function for the given ease type
11 /// </summary>
12 public static Func<float,float,float,float,float> easeFunctionForType( GoEaseType easeType )
13 {
14 switch( easeType )
15 {
16 case GoEaseType.Linear:
17 return GoEaseLinear.EaseNone;
18
19 case GoEaseType.BackIn:
20 return GoEaseBack.EaseIn;
21 case GoEaseType.BackOut:
22 return GoEaseBack.EaseOut;
23 case GoEaseType.BackInOut:
24 return GoEaseBack.EaseInOut;
25
26 case GoEaseType.BounceIn:
27 return GoEaseBounce.EaseIn;
28 case GoEaseType.BounceOut:
29 return GoEaseBounce.EaseOut;
30 case GoEaseType.BounceInOut:
31 return GoEaseBounce.EaseInOut;
32
33 case GoEaseType.CircIn:
34 return GoEaseCircular.EaseIn;
35 case GoEaseType.CircOut:
36 return GoEaseCircular.EaseOut;
37 case GoEaseType.CircInOut:
38 return GoEaseCircular.EaseInOut;
39
40 case GoEaseType.CubicIn:
41 return GoEaseCubic.EaseIn;
42 case GoEaseType.CubicOut:
43 return GoEaseCubic.EaseOut;
44 case GoEaseType.CubicInOut:
45 return GoEaseCubic.EaseInOut;
46
47 case GoEaseType.ElasticIn:
48 return GoEaseElastic.EaseIn;
49 case GoEaseType.ElasticOut:
50 return GoEaseElastic.EaseOut;
51 case GoEaseType.ElasticInOut:
52 return GoEaseElastic.EaseInOut;
53 case GoEaseType.Punch:
54 return GoEaseElastic.Punch;
55
56 case GoEaseType.ExpoIn:
57 return GoEaseExponential.EaseIn;
58 case GoEaseType.ExpoOut:
59 return GoEaseExponential.EaseOut;
60 case GoEaseType.ExpoInOut:
61 return GoEaseExponential.EaseInOut;
62
63 case GoEaseType.QuadIn:
64 return GoEaseQuadratic.EaseIn;
65 case GoEaseType.QuadOut:
66 return GoEaseQuadratic.EaseOut;
67 case GoEaseType.QuadInOut:
68 return GoEaseQuadratic.EaseInOut;
69
70 case GoEaseType.QuartIn:
71 return GoEaseQuartic.EaseIn;
72 case GoEaseType.QuartOut:
73 return GoEaseQuartic.EaseOut;
74 case GoEaseType.QuartInOut:
75 return GoEaseQuartic.EaseInOut;
76
77 case GoEaseType.QuintIn:
78 return GoEaseQuintic.EaseIn;
79 case GoEaseType.QuintOut:
80 return GoEaseQuintic.EaseOut;
81 case GoEaseType.QuintInOut:
82 return GoEaseQuintic.EaseInOut;
83
84 case GoEaseType.SineIn:
85 return GoEaseSinusoidal.EaseIn;
86 case GoEaseType.SineOut:
87 return GoEaseSinusoidal.EaseOut;
88 case GoEaseType.SineInOut:
89 return GoEaseSinusoidal.EaseInOut;
90 }
91
92 return GoEaseLinear.EaseNone;
93 }
94
95
96 /// <summary>
97 /// either returns a super fast Delegate to set the given property or null if it couldn't be found
98 /// via reflection
99 /// </summary>
100 public static T setterForProperty<T>( System.Object targetObject, string propertyName )
101 {
102 // first get the property
103 #if NETFX_CORE
104 var propInfo = targetObject.GetType().GetRuntimeProperty( propertyName );
105 #else
106 var propInfo = targetObject.GetType().GetProperty( propertyName );
107 #endif
108
109 if( propInfo == null )
110 {
111 Debug.Log( "could not find property with name: " + propertyName );
112 return default( T );
113 }
114
115 #if NETFX_CORE
116 // Windows Phone/Store new API
117 return (T)(object)propInfo.SetMethod.CreateDelegate( typeof( T ), targetObject );
118 #else
119 return (T)(object)Delegate.CreateDelegate( typeof( T ), targetObject, propInfo.GetSetMethod() );
120 #endif
121 }
122
123
124 /// <summary>
125 /// either returns a super fast Delegate to get the given property or null if it couldn't be found
126 /// via reflection
127 /// </summary>
128 public static T getterForProperty<T>( System.Object targetObject, string propertyName )
129 {
130 // first get the property
131 #if NETFX_CORE
132 var propInfo = targetObject.GetType().GetRuntimeProperty( propertyName );
133 #else
134 var propInfo = targetObject.GetType().GetProperty( propertyName );
135 #endif
136
137 if( propInfo == null )
138 {
139 Debug.Log( "could not find property with name: " + propertyName );
140 return default( T );
141 }
142
143 #if NETFX_CORE
144 // Windows Phone/Store new API
145 return (T)(object)propInfo.GetMethod.CreateDelegate( typeof( T ), targetObject );
146 #else
147 return (T)(object)Delegate.CreateDelegate( typeof( T ), targetObject, propInfo.GetGetMethod() );
148 #endif
149 }
150
151
152 #region math functions
153
154 /// <summary>
155 /// note for all lerps: normally a lerp would be something like the following:
156 /// val1 + ( val2 - val1 ) * t
157 /// or in more familiar terms:
158 /// start + ( end - start ) * t
159 ///
160 /// when lerping relatively, the formula simplifies to:
161 /// start + end * t
162 ///
163 /// for all the unclamped lerps in this class the diff value is precalculated and cached. that means these arent like normal
164 /// lerps where you pass in the start and end values. the "diff" paramter in each method should be either the cached
165 /// ( end - start ) for non-relative tweens or just end for relative tweens (that are not "from" tweens)
166 /// </summary>
167
168
169 /// <summary>
170 /// unclamped lerp from c1 to c2. diff should be c2 - c1 (or just c2 for relative lerps)
171 /// </summary>
172 public static Color unclampedColorLerp( Color c1, Color diff, float value )
173 {
174 return new Color
175 (
176 c1.r + diff.r * value,
177 c1.g + diff.g * value,
178 c1.b + diff.b * value,
179 c1.a + diff.a * value
180 );
181 }
182
183
184 /// <summary>
185 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
186 /// </summary>
187 public static Vector2 unclampedVector2Lerp( Vector2 v1, Vector2 diff, float value )
188 {
189 return new Vector2
190 (
191 v1.x + diff.x * value,
192 v1.y + diff.y * value
193 );
194 }
195
196
197 /// <summary>
198 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
199 /// </summary>
200 public static Vector3 unclampedVector3Lerp( Vector3 v1, Vector3 diff, float value )
201 {
202 return new Vector3
203 (
204 v1.x + diff.x * value,
205 v1.y + diff.y * value,
206 v1.z + diff.z * value
207 );
208
209 /*
210 return new Vector3
211 (
212 v1.x + ( v2.x - v1.x ) * value,
213 v1.y + ( v2.y - v1.y ) * value,
214 v1.z + ( v2.z - v1.z ) * value
215 );
216 */
217 }
218
219
220 /// <summary>
221 /// unclamped lerp from v1 to v2. diff should be v2 - v1 (or just v2 for relative lerps)
222 /// </summary>
223 public static Vector4 unclampedVector4Lerp( Vector4 v1, Vector4 diff, float value )
224 {
225 return new Vector4
226 (
227 v1.x + diff.x * value,
228 v1.y + diff.y * value,
229 v1.z + diff.z * value,
230 v1.w + diff.w * value
231 );
232 }
233
234 #endregion
235
236 }